<template>
	<a-card :bordered="false" class="mb-3 pl-4">
		<a-form ref="formRef" :model="formData" :rules="formRules">
			<a-form-item label="线索/客户联系人验重设置" name="contactsFlag">
				<a-radio-group v-model:value="formData.contactsFlag" :options="repeatOptions" />
			</a-form-item>
			<a-form-item label="老客户转介绍联系人验重设置" name="oldCustomerFlag">
				<a-radio-group v-model:value="formData.oldCustomerFlag" :options="repeatOptions" />
			</a-form-item>
			<div>
				<a-button class="mt-5" type="primary" :loading="formLoading" @click="onSubmit">保存</a-button>
			</div>
		</a-form>
	</a-card>
</template>

<script setup name="ContactEnterRule">
	import contactEnterRuleApi from '@/api/biz/crm/contactEnterRuleApi'
	import tool from '@/utils/tool'
	import { CommonDetermineEnum } from '@/utils/enum'

	const formLoading = ref(false)

	const formRef = ref()
	// 表单数据，也就是默认给一些数据
	const formData = ref({
		id: null,
		contactsFlag: CommonDetermineEnum.NO,
		oldCustomerFlag: CommonDetermineEnum.NO
	})
	// 默认要校验的
	const formRules = {}

	const repeatOptions = tool.dictList('COMMON_DETERMINE')

	onMounted(() => {
		contactEnterRuleApi.detail().then((data) => {
			if (data) {
				const { contactsFlag, oldCustomerFlag, ...other } = data
				formData.value = {
					...other,
					contactsFlag: contactsFlag || CommonDetermineEnum.NO,
					oldCustomerFlag: oldCustomerFlag || CommonDetermineEnum.NO
				}
			}
		})
	})

	const onSubmit = () => {
		formRef.value.validate().then(() => {
			formLoading.value = true
			contactEnterRuleApi
				.submitForm(formData.value)
				.then(() => {})
				.finally(() => {
					formLoading.value = false
				})
		})
	}
</script>

<style scoped></style>
